//
//  HOReportDetailEntity.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/11/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOReportDetailEntity.h"
#import "HOConstants.h"
#import "HOUtils.h"
#import "HOAreaEntity.h"
#import "HOStaticDataMgr.h"

@implementation HOReportDetailEntity


//@synthesize reporterName;
//@synthesize department;
//@synthesize phoneNumber;
//@synthesize location;
//@synthesize status;
//@synthesize detail;
//
//@synthesize imageList; //图片列表
//@synthesize voiceURL;  //音频URL
//@synthesize totalTime;  //音频总时间
//@synthesize hopeHandler;  //希望受理人
//@synthesize handler;  //受理人
//@synthesize category;  //故障分类
//@synthesize cost;  //费用
//@synthesize historyEntity;
- (instancetype)init
{
	self = [super init];
	if(self)
	{
		self.imageList = [[[NSMutableArray alloc] init] autorelease];
		self.repairerEntity = [[[HORepairerEntity alloc] init] autorelease];
		self.faultAreaList = [NSMutableArray array];
		self.faultAreaName = [NSMutableString string];
	}
	
	return self;
}

- (void)parseDic:(NSDictionary *)dic
{
	/*
	 
	 "OrderId": 1,//Int型 报修订单ID
	 "OrderSn":"2016060301",//String型 报修订单编号
	 "CreateTime":1464886868,//Int型 报修时间
	 "Status": 1,//Int型 报修状态【1:未处理2:已处理3:维修完成4:确认完成】
	 "AreaName":"",//String型 报修区域名称
	 "faultArea":"",//String型 报修区域名称 json arrray
	 "FaultDetail":""//String型 故障详情
	 "FaultImage":"" //String型 故障图片,
	 "Money":"",//String型 维修费用,
	 "HopeName":"",//String型 希望受理人,
	 "DoneName":"",//String型  维修人员,
	 "FaultName":"",//String型 故障分类名称,
	 "FaultVoice":"",//String型 报修语音,
	 "PostName": "王远庆",//String型 报修人姓名
	 "Mobile": "18757156045",//String型 报修人电话
	 "DepartmentName": "技术部",//String型 报修人所在部门名称
	 "Comment":{},//Object型 评论列表,
	 */
	self.faultAreaName = [NSMutableString string];
	self.department = [dic objectForKey:kHO_FIELD_DepartmentName];
	self.detail = [HOUtils getValideString:[dic objectForKey:kHO_FIELD_FaultDetail]];
//	self.detail = @"这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容";
	self.phoneNumber = [dic objectForKey:kHO_FIELD_Mobile];
	self.reporterName = [dic objectForKey:kHO_FIELD_PostName];
	self.voiceURL = [dic objectForKey:kHO_FIELD_FaultVoice];
	self.category = [dic objectForKey:kHO_FIELD_FaultName];
	self.handler = [dic objectForKey:kHO_FIELD_DoneName];
	self.hopeHandler = [dic objectForKey:kHO_FIELD_HopeName];
	self.cost = [[dic objectForKey:kHO_FIELD_Money] floatValue];
	self.imageList = [dic objectForKey:kHO_FIELD_FaultImage];
	self.status = [[dic objectForKey:kHO_FIELD_Status] intValue];
	self.isUncompleted = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_IsIncomplete]] boolValue];

	self.owerId = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_OwerId] ]intValue];
	self.isAssistantReport = [[dic objectForKey:kHO_FIELD_AssistantData] boolValue];
	[self parseAreas:dic];
	[self parseTypes:dic];
	[self parseDepartments:[dic objectForKey:kHO_FIELD_FaultDepartment]];
	self.postDepartmentName = [dic objectForKey:kHO_FIELD_PostDepartmentName];
	self.postDepartmentId = [[dic objectForKey:kHO_FIELD_PostDid] intValue];
	self.faultIds = [HOUtils getValideString:[dic objectForKey:kHO_FIELD_FaultId]];
	self.createTime = [[dic objectForKey:kHO_FIELD_CreateTime] doubleValue];
	self.orderSN = [[dic objectForKey:kHO_FIELD_OrderSn] intValue];
	self.status = [[dic objectForKey:kHO_FIELD_Status] intValue];
	self.postId = [[dic objectForKey:kHO_FIELD_PostId] intValue];
	self.doneId = [[dic objectForKey:kHO_FIELD_DoneId] intValue];
	self.faultDepartmentId = [[dic objectForKey:kHO_FIELD_FaultDepartmentId] intValue];
	self.totalTime = [[dic objectForKey:kHO_FIELD_FaultVoiceTime] doubleValue];
	self.repairerEntity.userId = self.doneId;
	self.repairerEntity.userName = self.handler;
	self.virtualCellPhone = [HOUtils getValideString:[dic objectForKey:kHO_FIELD_VirtualCellPhone]];
	self.isUrgent = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_Isurgent]] boolValue];
	self.isUncompleted = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_IsIncomplete]] boolValue];
	self.repairerEntity.HopeName = [dic objectForKey:kHO_FIELD_DoneName];
	self.isResubmit = [[dic objectForKey:kHO_FIELD_isReSubmit_low] intValue] == 1;
	NSDictionary *commentDic = [dic objectForKey:kHO_FIELD_Comment];
	if(self.status == HO_REPORT_ORDER_ACCEPTED && self.isUncompleted)
	{
		self.status = HO_REPORT_ORDER_UNCOMPLETED;
	}
	if(commentDic && [commentDic allKeys].count > 0)
	{
		self.commentEntity = [[[HOCommentEntity alloc] init] autorelease];
		[self.commentEntity parseDic:commentDic];
	}
	else
	{
		self.commentEntity = nil;
	}
}

- (void)parseDepartments:(NSDictionary *)dic
{
	if(dic == nil)
		return;
	int departmentId = [[dic objectForKey:kHO_FIELD_DepartmentId] intValue];
	self.firstDepartmentEntity = [[HOStaticDataMgr sharedInstance] getDepartmentEntityWithId:departmentId];
	[self.firstDepartmentEntity parseDic:dic];
}

- (void)parseTypes:(NSDictionary *)dic
{
	if(dic == nil)
		return;
	NSArray *typeList = [dic objectForKey:kHO_FIELD_FaultType];
	if(typeList != nil && typeList.count > 0)
	{
		for(int i = 0; i < [typeList count]; i ++)
		{
			NSDictionary *subDic = [typeList objectAtIndex:i];
			int subFaultId = [[HOUtils getValideString:[subDic objectForKey:kHO_FIELD_FaultId]] intValue];
			if(subFaultId == 0)
				continue;
			HOFaultCategoryEntity *entity = [[HOStaticDataMgr sharedInstance] getFaultCategoryEntityWithId:subFaultId];
			if(!entity)
			{
				continue;
			}
			[entity parseDic:subDic];
			
			if(i == 0)
				self.firstTypeEntity = entity;
			else if(i == 1)
				self.secondTypeEntity = entity;
		}
	}
}
- (void)parseAreas:(NSDictionary *)dic
{
	if(dic == nil)
		return;
	NSArray *areaList = [dic objectForKey:kHO_FIELD_FaultArea];
	if(areaList != nil && areaList.count > 0)
	{
		for(int i = 0; i < [areaList count]; i ++)
		{
			NSDictionary *areaDic = [areaList objectAtIndex:i];
			int areaId = [[HOUtils getValideString:[areaDic objectForKey:kHO_FIELD_AreaId]] intValue];
			NSString *areaName = [HOUtils getValideString:[areaDic objectForKey:kHO_FIELD_AreaName]];
			if(areaId == 0 || areaName == nil || areaName.length == 0)
				continue;
			HOAreaEntity *areaEntity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:areaId];
			if(!areaEntity)
			{
				continue;
			}
			areaEntity.areaName = areaName;
			[self.faultAreaList addObject:areaEntity];
			if(self.faultAreaName.length > 0)
			{
				[self.faultAreaName appendFormat:@"-%@", areaName];
			}
			else
			{
				[self.faultAreaName appendFormat:@"%@", areaName];
			}
			if(i == 0)
				self.firstAreaEntity = areaEntity;
			else if(i == 1)
				self.secondAreaEntity = areaEntity;
			else if(i == 2)
				self.thirdAreaEntity = areaEntity;
		}
	}
}

- (void)dealloc
{
	self.firstAreaEntity = nil;
	self.secondAreaEntity = nil;
	self.thirdAreaEntity = nil;
	self.firstDepartmentEntity = nil;
	self.secondDepartmentEntity = nil;
	self.firstTypeEntity = nil;
	self.secondAreaEntity = nil;
	self.department = nil;
	self.phoneNumber = nil;
	self.faultAreaList = nil;
	self.status = 0;
	self.detail = nil;
	self.imageList = nil;
	self.voiceURL = nil;
	self.totalTime = 0;
	self.hopeHandler = nil;
	self.handler = nil;
	self.category = nil;
	self.commentEntity = nil;
	self.repairerEntity = nil;
	[super dealloc];
}
@end
